
using Youz.Common.DBUtility;
using Youz.Common.Entity;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace Youz.Common.DAL
{
    /// <summary>
    /// CMS_LeaveWordsDetails Data Class
    /// </summary>
    public partial class CMS_LeaveWordsDetailsDAL : BaseDAL
    {
		private DbHelperSQL DbHelperSQL = new DbHelperSQL();
        public string TableName = "CMS_LeaveWordsDetails";

        public CMS_LeaveWordsDetailsDAL()
        {}

        #region  成员方法
        /// <summary>
        ///  新增一条数据
        /// </summary>
        public bool Add(CMS_LeaveWordsDetails model)
        {
            StringBuilder strSql = new StringBuilder();
			
            strSql.Append("insert into [CMS_LeaveWordsDetails](");
            strSql.Append("[DetailsID],[leaveWordsID],[Content],[Creator],[CreateTime],[IsAdmin],[Img],[IsRead]");
            strSql.Append(") values (");
            strSql.Append("@DetailsID,@leaveWordsID,@Content,@Creator,@CreateTime,@IsAdmin,@Img,@IsRead");
            strSql.Append(")");
			
            SqlParameter[] parameters = {
                  
                    new SqlParameter("@DetailsID", model.DetailsID),
                    new SqlParameter("@leaveWordsID", model.leaveWordsID),
                    new SqlParameter("@Content", model.Content),
                    new SqlParameter("@Creator", model.Creator),
                    new SqlParameter("@CreateTime", model.CreateTime),
                    new SqlParameter("@IsAdmin", model.IsAdmin),
                    new SqlParameter("@Img", model.Img),
                    new SqlParameter("@IsRead", model.IsRead)
            };

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        ///  保存一条数据
        /// </summary>
        public bool Update(CMS_LeaveWordsDetails model)
        {
            StringBuilder strSql = new StringBuilder();
			
            strSql.Append("update [CMS_LeaveWordsDetails] set ");
            strSql.Append("[leaveWordsID]=@leaveWordsID,[Content]=@Content,[Creator]=@Creator,[CreateTime]=@CreateTime,[IsAdmin]=@IsAdmin,[Img]=@Img,[IsRead]=@IsRead");
            strSql.Append(" where [DetailsID]=@DetailsID");
			
            SqlParameter[] parameters = {
                  
                    new SqlParameter("@DetailsID", model.DetailsID),
                    new SqlParameter("@leaveWordsID", model.leaveWordsID),
                    new SqlParameter("@Content", model.Content),
                    new SqlParameter("@Creator", model.Creator),
                    new SqlParameter("@CreateTime", model.CreateTime),
                    new SqlParameter("@IsAdmin", model.IsAdmin),
                    new SqlParameter("@Img", model.Img),
                    new SqlParameter("@IsRead", model.IsRead)
            };

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        ///</summary>
        public bool Delete(Guid DetailsID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [CMS_LeaveWordsDetails] ");
            strSql.Append(" where [DetailsID]=@DetailsID");
			
            SqlParameter[] parameters = {
			
                new SqlParameter("@DetailsID", DetailsID)             
              
            };

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CMS_LeaveWordsDetails Get(Guid DetailsID)
        {
            StringBuilder strSql = new StringBuilder();
			
            strSql.Append("select  top 1 * from [CMS_LeaveWordsDetails] ");
            strSql.Append(" where [DetailsID]=@DetailsID");
			
            SqlParameter[] parameters = {
			
                new SqlParameter("DetailsID", DetailsID)             
              
            };

            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return Get(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

		/// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CMS_LeaveWordsDetails Get(DataRow row)
        {
            if (row == null) return null;

            CMS_LeaveWordsDetails model = new CMS_LeaveWordsDetails();
	
            
            if (row["DetailsID"] != DBNull.Value)
            {
                model.DetailsID = (Guid)row["DetailsID"];
            }      
            
            if (row["leaveWordsID"] != DBNull.Value)
            {
                model.leaveWordsID = (Guid)row["leaveWordsID"];
            }      
            
            if (row["Content"] != DBNull.Value)
            {
                model.Content = (String)row["Content"];
            }      
            
            if (row["Creator"] != DBNull.Value)
            {
                model.Creator = (String)row["Creator"];
            }      
            
            if (row["CreateTime"] != DBNull.Value)
            {
                model.CreateTime = (DateTime)row["CreateTime"];
            }      
            
            if (row["IsAdmin"] != DBNull.Value)
            {
                model.IsAdmin = (Boolean)row["IsAdmin"];
            }      
            
            if (row["Img"] != DBNull.Value)
            {
                model.Img = (String)row["Img"];
            }      
            
            if (row["IsRead"] != DBNull.Value)
            {
                model.IsRead = (Boolean)row["IsRead"];
            }      
            

            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet Gets(string sqlWhere)
        {
            StringBuilder strSql = new StringBuilder();
			
            strSql.Append("select * from [CMS_LeaveWordsDetails] ");
            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }
            
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet Gets(int Top, string sqlWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" *  FROM [CMS_LeaveWordsDetails] ");
			
            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }
            if(filedOrder.Trim() != "")
            {
                strSql.Append(" order by " + filedOrder);
            }
			
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string sqlWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM [CMS_LeaveWordsDetails] ");
            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 获得数据列表分页
        /// </summary>
        public DataTable Gets(string sqlWhere, int pageindex, int pagesize, out int total, params SqlParameter[] cmdParms)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append(" SELECT * FROM [CMS_LeaveWordsDetails]  ");
            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }

            int count = 0;
            DataTable dt = DbHelperSQL.GetPager(pageindex, pagesize, "" + strSql + "", "order by DetailsID desc ", out count, cmdParms);
            total = count;
            return dt;
        }
      #endregion  成员方法

        /// <summary>
        /// 修改已读状态
        /// </summary>
        /// <param name="leaveWordsID"></param>
        /// <param name="isAdmin"></param>
        /// <returns></returns>
        public bool UpdateIsRead(string leaveWordsID, bool isAdmin)
        {
            string sql = string.Format(@"update [dbo].[CMS_LeaveWordsDetails] set IsRead=1
                                        where leaveWordsID='{0}' and IsAdmin={1}", leaveWordsID, isAdmin ? 1 : 0);

            int rows = DbHelperSQL.ExecuteSql(sql);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}


  